﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShopOnline.Models.Entities;

namespace ShopOnline.Models.DAL
{
    public class DAL_Catelogy
    {
        private ShopOnline.Models.ShopOnlineDBDataContext SC_DB;
        public DAL_Catelogy()
        {
            SC_DB = new ShopOnlineDBDataContext();
        }


        public List<Catelogy> getListCatelogy()
        {
           return SC_DB.Catelogies.ToList();
        }

        public List<Catelogy> getListCatelogyIsShow()
        {
            return (from c in SC_DB.Catelogies  orderby c.piority where c.show == true orderby c.piority  select c).ToList();
        }

        public List<Catelogy> getListCatelogyChild(int CatelogyId){
           var listCate= (from c in SC_DB.Catelogies where c.fartherId==CatelogyId select c).ToList();
            return listCate;
        }

        public List<Press> getListPressByCateId(int CatelogyId)
        {
            var listPress = (from p in SC_DB.Presses where p.catelogyId == CatelogyId select p).ToList();
            return listPress;
        }

        public List<Press> getListPressByCateIdIsShow(int CatelogyId)
        {
            var listPress = (from p in SC_DB.Presses orderby p.dateCreate where p.catelogyId == CatelogyId && p.show == true orderby p.piority  select p).ToList();
            return listPress;
        }

        public Catelogy getCatelogyById(int catelogyId)
        {
            return (from c in SC_DB.Catelogies where c.catelogyId==catelogyId select c).FirstOrDefault();
        }

        public int CreateCatlogy(Catelogy cate)
        {
            int result = 0;
            try
            {
                SC_DB.Catelogies.InsertOnSubmit(cate);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }

        public int EditCatlogy(Catelogy cate)
        {
            int result = 0;
            try
            {
                var catelogy = (from c in SC_DB.Catelogies where c.catelogyId == cate.catelogyId select c).FirstOrDefault();
                catelogy.catelogyName = cate.catelogyName;
                catelogy.body = cate.body;
                catelogy.curent = cate.curent;
                catelogy.description = cate.description;
                catelogy.enableModifyChirldren = cate.enableModifyChirldren;
                catelogy.fartherId = cate.fartherId;
                catelogy.imagePath = cate.imagePath;
                catelogy.piority = cate.piority;
                catelogy.show = cate.show;
                catelogy.url = cate.url;
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }
        public int DeleteCatelogy(Catelogy cate)
        {
            int result = 0;
            try
            {
                var catelogy = (from c in SC_DB.Catelogies where c.catelogyId == cate.catelogyId select c).FirstOrDefault();
                SC_DB.Catelogies.DeleteOnSubmit(catelogy);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }


        // duy add

        public List<Press> get5ListPressByCateId(int CatelogyId,int pressid)
        {
            var listPress = (from p in SC_DB.Presses where p.catelogyId == CatelogyId && p.show == true && p.pressId!=pressid orderby p.piority select p).Take(5).ToList(); 
            return listPress;
        }
        public List<Press> get5ListPressNewByCateId()
        {
            var listPress = (from p in SC_DB.Presses where p.show == true orderby  p.dateCreate,p.piority select p).Take(5).ToList();
            return listPress;
        }
        public List<MenuItem> getListMenuItem()
        {
            List<MenuItem> listMenu = new List<MenuItem>();
            List<Catelogy> listCatelogy = getListCatelogyIsShow();
            foreach (var item in listCatelogy)
            {
                MenuItem menuItem = new MenuItem();
                menuItem.Catelogy = item;
                List<Press> listPress = getListPressByCateIdIsShow(item.catelogyId);
                menuItem.ListPress = listPress;

                listMenu.Add(menuItem);
            }
            return listMenu;
        }

    
    }
}